import { formatTime } from "./date.ts";
import { decodeText } from "./decodeText";
import { bigEmojiList } from "./emojiMap";
import {
	useI18n
} from "vue-i18n";


// 处理头像
export function handleAvatar(item: any) {
  let avatar = "";
  if (item.type === uni.$TIM.TYPES.CONV_C2C) {
    avatar =
      item?.userProfile?.avatar ||
      "https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png";
  } else {
    avatar =
      item?.groupProfile?.avatar ||
      "https://web.sdk.qcloud.com/component/TUIKit/assets/group_avatar.png";
  }
  return avatar;
}

// 处理昵称
export function handleName(item: any) {
  let name = "";
  switch (item.type) {
    case uni.$TIM.TYPES.CONV_C2C:
      name = item?.userProfile.nick || item?.userProfile?.userID || "";
      break;
    case uni.$TIM.TYPES.CONV_GROUP:
      name = item.groupProfile.name || item?.groupProfile?.groupID || "";
      break;
    case uni.$TIM.TYPES.CONV_SYSTEM:
      name = "系统通知";
      break;
  }
  return name;
}

// 处理系统提示消息展示
export function handleTipMessageShowContext(message: any) {
  const options: any = {
    message,
    text: "",
  };
  console.log("fffffffffffffffffffffffffffffffffff",message)
  const userName = message.nick || message.payload.userIDList.join(",");
  const {
  	t
  } = useI18n();
  switch (message.payload.operationType) {
    case uni.$TIM.TYPES.GRP_TIP_MBR_JOIN:
     options.text = `${userName}`+t('chat.message_group_notify[10]'); // `群成员： 加入群组`
      break;
    case uni.$TIM.TYPES.GRP_TIP_MBR_QUIT:
      options.text =  t('chat.message_group_notify[11]')+`${userName}` ;//`群成员：${userName} 退出群组`
      break;
    case uni.$TIM.TYPES.GRP_TIP_MBR_KICKED_OUT:
      options.text =  t('chat.message_group_notify[12]'+`${userName}`) ;// `群成员：${userName} 被 ${message.payload.operatorID} 踢出群组`
      break;
    case uni.$TIM.TYPES.GRP_TIP_MBR_SET_ADMIN:
      options.text = `群成员：${userName} 成为管理员`;
      break;
    case uni.$TIM.TYPES.GRP_TIP_MBR_CANCELED_ADMIN:
      options.text = `群成员：${userName} 被撤销管理员`;
      break;
    case uni.$TIM.TYPES.GRP_TIP_GRP_PROFILE_UPDATED:
      // options.text =  `${userName} 修改群组资料`;

      options.text = handleTipGrpUpdated(message);
      break;
    case uni.$TIM.TYPES.GRP_TIP_MBR_PROFILE_UPDATED:
      for (const member of message.payload.memberList) {
        if (member.muteTime > 0) {
          // options.text =  `群成员：${member.userID}被禁言${member.muteTime}秒`;
          options.text = `群成员：${member.userID} 被禁言`;
        } else {
          options.text = `群成员：${member.userID} 被取消禁言`;
        }
      }
      break;
    default:
      options.text = `[群提示消息]`;
      break;
  }
  return options;
}

function handleTipGrpUpdated(message: any) {
	
  const { payload } = message;
  const { newGroupProfile } = payload;
  const { operatorID } = payload;
  let text = "";
  const {
	  t
  } = useI18n();
  const name = Object.keys(newGroupProfile)[0];
  // console.log("看看 newGroupProfile 里面都有什么东西 ，获取一下公告信息",)
  switch (name) {
    case "ownerID":
      text = `${newGroupProfile[name]}`+t('chat.message_group_notify[15]');// 成为新的群主
      break;
    case "groupName":
      text = t('chat.message_group_notify[13]')+` ${newGroupProfile[name]}`;//修改群名称为
      break;
    case "notification":
      text = t('chat.message_group_notify[19]')+`: ${newGroupProfile[name]}`;//发布新公告
      break;
	case "avatar":
	  text=`更换了新群聊的头像`;
	  break;
    default:
      break;
  }
  return text;
}

// 解析处理文本消息展示
export function handleTextMessageShowContext(item: any) {
  const options: any = {
    text: decodeText(item.payload),
  };
  return options;
}
//解析处理合并文本消息展示
export function handleTextMessageShowContext1(item: any) {
	console.log('进来这里了吗++++++++++++++++++++++++',JSON.stringify(item))
  const options: any = {
    text: decodeText(item.messageBody[0].payload),
  };
  return options;
}
// 解析处理表情消息展示
export function handleFaceMessageShowContext(item: any) {
  const face: any = {
    message: item,
    name: "",
    url: "",
  };
  const currentEmojiList = bigEmojiList.filter(
    (emoItem: any) => emoItem.icon === item.payload.data
  );
  if (currentEmojiList.length > 0) {
    face.name = currentEmojiList[0].list[item.payload.index];
  }
  // 与native 互通兼容
  if (item.payload.data.indexOf("@2x") > 0) {
    face.name = item.payload.data;
  } else {
    face.name = `${item.payload.data}@2x`;
  }
  face.url = `https://web.sdk.qcloud.com/im/assets/face-elem/${face.name}.png`;
  return face;
}

// 解析处理位置消息展示
export function handleLocationMessageShowContext(item: any) {
  const location: any = {
    lon: "",
    lat: "",
    href: "",
    url: "",
    description: "",
    message: item,
  };
  location.lon = item.payload.longitude.toFixed(6);
  location.lat = item.payload.latitude.toFixed(6);
  location.href =
    "https://map.qq.com/?type=marker&isopeninfowin=1&markertype=1&" +
    `pointx=${location.lon}&pointy=${location.lat}&name=${item.payload.description}`;
  location.url =
    "https://apis.map.qq.com/ws/staticmap/v2/?" +
    `center=${location.lat},${location.lon}&zoom=10&size=300*150&maptype=roadmap&` +
    `markers=size:large|color:0xFFCCFF|label:k|${location.lat},${location.lon}&` +
    "key=UBNBZ-PTP3P-TE7DB-LHRTI-Y4YLE-VWBBD";
  location.description = item.payload.description;
  return location;
}

// 解析处理图片消息展示
export function handleImageMessageShowContext(item: any) {
  return {
    progress: item?.status === "unSend" && item.progress,
    info: item.payload.imageInfoArray,
    message: item,
  };
}
//解析处理合并图片消息
export function handleImageMessageShowContext1(item: any) {
	console.log(JSON.stringify(item))
  return {
    progress: item?.status === "unSend" && item.progress,
    info: item.messageBody[0].payload.imageInfoArray,
    message: item,
  };
}

// 解析处理视频消息展示
export function handleVideoMessageShowContext(item: any) {
  return {
    progress: item?.status === "unSend" && item?.progress,
    url: item?.payload?.videoUrl,
    snapshotUrl: item?.payload?.snapshotUrl,
    message: item,
  };
}
//解析处理合并视频消息
export function handleVideoMessageShowContext1(item: any) {
	console.log(JSON.stringify(item))
  return {
    url: item.messageBody[0].payload.videoUrl,
    snapshotUrl: item?.payload?.snapshotUrl,
    message: item,
  };
}

// 解析处理语音消息展示
export function handleAudioMessageShowContext(item: any) {
  return {
    progress: item?.status === "unSend" && item.progress,
    url: item.payload.url,
    message: item,
    second: item.payload.second,
  };
}
// 解析处理语音消息展示
export function handleAudioMessageShowContext1(item: any) {
  return {
    progress: item?.status === "unSend" && item.progress,
    url: item.messageBody[0].payload.url,
    message: item,
    second: item.messageBody[0].payload.second,
  };
}
// 解析处理文件消息展示
export function handleFileMessageShowContext(item: any) {
  let size = "";
  if (item.payload.fileSize >= 1024 * 1024) {
    size = `${(item.payload.fileSize / (1024 * 1024)).toFixed(2)} Mb`;
  } else if (item.payload.fileSize >= 1024) {
    size = `${(item.payload.fileSize / 1024).toFixed(2)} Kb`;
  } else {
    size = `${item.payload.fileSize.toFixed(2)}B`;
  }
  return {
    progress: item?.status === "unSend" && item.progress,
    url: item.payload.fileUrl,
    message: item,
    name: item.payload.fileName,
    size,
  };
}
//解析处理合并文件消息展示
export function handleFileMessageShowContext1(item: any) {
	console.log('sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss',JSON.stringify(item))
  let size = "";
  if (item.messageBody[0].payload.fileSize >= 1024 * 1024) {
    size = `${(item.messageBody[0].payload.fileSize / (1024 * 1024)).toFixed(2)} Mb`;
  } else if (item.messageBody[0].payload.fileSize >= 1024) {
    size = `${(item.messageBody[0].payload.fileSize / 1024).toFixed(2)} Kb`;
  } else {
    size = `${item.messageBody[0].payload.fileSize.toFixed(2)}B`;
  }
  return {
    url: item.messageBody[0].payload.fileUrl,
    message: item,
    name: item.messageBody[0].payload.fileName,
    size,
	progress: item?.status === "unSend" && item.progress,
  };
}

// 解析处理合并消息展示
export function handleMergerMessageShowContext(item: any) {
  if (item.type === uni.$TIM.TYPES.MSG_MERGER && item.payload.downloadKey !== '') {
    let promise = uni.$TUIKit.downloadMergerMessage(item);
    promise.then(function(imResponse) {
      // 下载成功后，SDK会更新 message.payload.messageList 等信息
      console.log(imResponse.data);
    }).catch(function(imError) {
      // 下载失败
      console.warn('downloadMergerMessage error:', imError);
    });
  }
  return { message: item, ...item.payload };
}

// 解析音视频通话消息
export function extractCallingInfoFromMessage(message: any) {
  let callingmessage: any = {};
  let objectData: any = {};
  const {
  	t
  } = useI18n();
  try {
    callingmessage = JSON.parse(message.payload.data);
  } catch (error) {
    callingmessage = {};
  }
  if (callingmessage.businessID !== 1) {
    return "";
  }
  try {
    objectData = JSON.parse(callingmessage.data);
  } catch (error) {
    objectData = {};
  }

  switch (callingmessage.actionType) {
    case 1: {		 
      if (objectData.call_end >= 0 && !callingmessage.groupID) {
		 return t('chat.calling_call_duration')+`：${formatTime(objectData.call_end)}`;//通话时长
      }else if(objectData.call_end >= 0 && callingmessage.groupID){
		return t('chat.calling_leaves') 
	  }else {
		  return t('chat.calling_initiate_call');//发起通话
	  }
    }
    case 2:
      return t('chat.calling_cancel_call');//取消通话
    case 4:
      return t('chat.calling_reject_call');//拒绝通话
     case 5:
	  return t('chat.calling_no_answer_call');//`无应答`
  }
}

// 解析处理自定义消息展示
export function handleCustomMessageShowContext(item: any) {
	// console.log("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",item)
  return {
    message: item,
    custom:
      extractCallingInfoFromMessage(item),
  };
}

// 解析处理系统消息
export function translateGroupSystemNotice(message: any) {
	const {
		t
	} = useI18n();
  const groupName =
    message.payload.groupProfile?.name || message.payload.groupProfile?.groupID;
  switch (message.payload.operationType) {
    case 1:
      return `${message.payload.operatorID} 申请加入群组：${groupName}`;
    case 2:
	
      return  t('message_group_notify'[1]);
    case 3:
      return `申请加入群组：${groupName} 被拒绝`;
    case 4:
      return `你被管理员 ${message.payload.operatorID} 踢出群组：${groupName}`;
    case 5:
      return `群：${groupName} 被 ${message.payload.operatorID} 解散`;
    case 6:
      return `${message.payload.operatorID} 创建群：${groupName}`;
    case 7:
      return `${message.payload.operatorID} 邀请你加群：${groupName}`;
    case 8:
      return `你退出群组：${groupName}`;
    case 9:
      return `你被${message.payload.operatorID} 设置为群：${groupName} 的管理员`;
    case 10:
      return `你被 ${message.payload.operatorID} 撤销群：${groupName} 的管理员身份`;
    case 12:
      return `${message.payload.operatorID} 邀请你加群：${groupName}`;
    case 13:
      return `${message.payload.operatorID} 同意加群 ：${groupName}`;
    case 14:
      return `${message.payload.operatorID} 拒接加群 ：${groupName}`;
    case 255:
      return `自定义群系统通知: ${message.payload.userDefinedField}`;
  }
}
